#include "rk3588_i2c_slave.h"
#define I2C_BUFFER_SIZE 10
uint8_t rx_buffer[I2C_BUFFER_SIZE] = {0};
uint8_t receive_flag = 0;
uint8_t soft_version[12] = {0xEB, 0x90, 0xA1, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00};

void DataParse_Init(void)
{
    HAL_I2C_Slave_Receive_IT(&hi2c2, rx_buffer, I2C_BUFFER_SIZE);
}

void DataParse_Print(void)
{
    if (receive_flag == 1)
    {
        printf("received:   \n");
        for (uint8_t i = 0; i < I2C_BUFFER_SIZE; i++)
        {
            printf("BUFFER %d: %d\n", i, rx_buffer[i]);
        }
        receive_flag = 0;
    }
}
static void I2C_SendData(uint8_t *data, uint16_t len)
{
    HAL_I2C_Slave_Transmit(&hi2c2, data, len, HAL_MAX_DELAY);
}

static uint8_t ParseSensorData(uint8_t *data, uint8_t count)
{
    uint8_t *frame;
    uint16_t crc;
    // static
    if (data[0] != 0xEB || data[1] != 0x90)
    {
        return 99;
    }

    switch (data[2])
    {
    case 1:
        if (data[3] != 0x02)
        {
            return 99;
        }
        soft_version[8] = (uint8_t)(frame_count >> 8);   // 高 8 位
        soft_version[9] = (uint8_t)(frame_count & 0xFF); // 低 8 位
        frame_count++;
        SoftVersion(soft_version);
        I2C_SendData(soft_version, 12);
        break;
    case 2:
        if (data[3] != 0x02)
        {
            return 99;
        }

        frame = GetFrame1();
        frame[8] = (uint8_t)(frame_count >> 8);   // 高 8 位
        frame[9] = (uint8_t)(frame_count & 0xFF); // 低 8 位
        frame_count++;
        crc = crc16(frame, FRAME_HEADER_SIZE + FRAME_DATA_SIZE_1 + 2); // 计算前 10 个字节的 CRC
        frame[11] = (uint8_t)(crc >> 8);                               // CRC 高字节
        frame[10] = (uint8_t)(crc & 0xFF);                             // CRC 低字节
        I2C_SendData(frame, 12);
        break;
    case 3:
        if (data[3] != 0x02)
        {
            return 99;
        }
        frame = GetFrame2();
        frame[28] = (uint8_t)(frame_count >> 8);   // 高 8 位
        frame[29] = (uint8_t)(frame_count & 0xFF); // 低 8 位
        frame_count++;
        crc = crc16(frame, 30);            // 计算前 30 个字节的 CRC
        frame[31] = (uint8_t)(crc >> 8);   // CRC 高字节
        frame[30] = (uint8_t)(crc & 0xFF); // CRC 低字节
        I2C_SendData(frame, 32);
        break;
    case 4:
        if (data[3] != 0x04)
        {
            return 99;
        }
        frame_count++;
        SetPins(data[4], data[5]);
        break;
    case 9:
        break;
    default:
        return 99;
    }
    return 0;
}

static void IIC_IRQHandler(uint8_t *data)
{
    static uint8_t count = 1;
    if (ParseSensorData(data, count) == 99)
    {
        return;
    }
    count++;
}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
    receive_flag = 1;
    IIC_IRQHandler(rx_buffer);
    HAL_I2C_Slave_Receive_IT(&hi2c2, rx_buffer, I2C_BUFFER_SIZE);
}

void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle)
{
    printf("error type  %x \n", HAL_I2C_GetError(I2cHandle));
    receive_flag = 1;
    IIC_IRQHandler(rx_buffer);
    HAL_I2C_DeInit(I2cHandle);
    MX_I2C2_Init();
    HAL_I2C_Slave_Receive_IT(I2cHandle, rx_buffer, I2C_BUFFER_SIZE);
}
